﻿Namespace Buddies
    Friend Class FSelectBuddy
        Private mResult As New Generic.List(Of Entities.Buddy)
        Private mOptions As SelectBuddiesOptions

        Public Shadows Function ShowDialog(ByVal owner As Form, ByVal options As SelectBuddiesOptions) As Generic.List(Of Entities.Buddy)
            mOptions = options

            For Each clsBud As Entities.Buddy In Handler.GetAll
                UpdateBuddy(clsBud)
            Next

            MyBase.ShowDialog(owner)
            Return mResult
        End Function

        Private Sub UpdateBuddy(ByVal buddy As Entities.Buddy)
            Dim strKey As String = buddy.Id.ToString
            If lvwBuddies.Items.ContainsKey(strKey) Then
                With lvwBuddies.Items(strKey)
                    .Text = buddy.NameLocal
                    .SubItems(1).Text = buddy.Comment
                    .Tag = buddy
                End With
            ElseIf Not mOptions.HideBuddyIds.Contains(buddy.Id) Then
                With lvwBuddies.Items.Add(strKey, buddy.NameLocal, CStr(Nothing))
                    .SubItems.Add(buddy.Comment)
                    .Tag = buddy
                End With
            End If
        End Sub

        Private Sub EventHandler_Ok(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click, lvwBuddies.DoubleClick
            Try
                For Each clsItem As ListViewItem In lvwBuddies.SelectedItems
                    Dim clsBud As Entities.Buddy = CType(clsItem.Tag, Entities.Buddy)
                    mResult.Add(clsBud)
                Next
                Me.Close()
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub
        Private Sub EventHandler_BuddyUpdated(ByVal buddy As Entities.Buddy)
            Try
                UpdateBuddy(buddy)
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub
        Private Sub EventHandler_BuddyDeleted(ByVal buddy As Entities.Buddy)
            Try
                Dim strKey As String = buddy.Id.ToString
                If lvwBuddies.Items.ContainsKey(strKey) Then lvwBuddies.Items.RemoveByKey(strKey)
            Catch ex As Exception
                Framework.Misc.HandleException(Me, ex)
            End Try
        End Sub


        Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            Me.Close()
        End Sub

        Private Sub FSelectBuddy_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            RemoveHandler Framework.Buddies.Manager.UpdatedGUI, AddressOf EventHandler_BuddyUpdated
            RemoveHandler Framework.Buddies.Manager.DeletedGUI, AddressOf EventHandler_BuddyDeleted
        End Sub

        Private Sub FSelectBuddy_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            AddHandler Framework.Buddies.Manager.UpdatedGUI, AddressOf EventHandler_BuddyUpdated
            AddHandler Framework.Buddies.Manager.DeletedGUI, AddressOf EventHandler_BuddyDeleted
        End Sub
    End Class
End Namespace